/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trefliq.EJB.sessionbeans;

import java.util.List;
import javax.annotation.security.DeclareRoles;
import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import trefliq.EJB.entities.Rating;
import trefliq.EJB.entities.Rating;
import trefliq.EJB.security.SecurityRole;

/**
 *
 * @author Mike Sesterhenn
 */
@Stateless
@LocalBean
@DeclareRoles({SecurityRole.ADMIN, SecurityRole.EMPLOYEE, SecurityRole.CUSTOMER})
@PermitAll
public class RatingBean extends AbstractFacade<Rating> {
    
    @PersistenceContext(unitName="trefliqPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() { return em; }
   
    public RatingBean() {
        super(Rating.class);
    }

    @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public String testAccess() {
        return "Access test successful.";
    }

    // DB helper methods
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public void deleteAll() {
        Query q = em.createQuery("DELETE FROM Rating o");
        q.executeUpdate();
    }

    // AbstractFacade methods.  Copied in to apply security attributes.
    
    @Override
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public void create(Rating entity) {
        super.create(entity);
    }

    @Override
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public void edit(Rating entity) {
        super.edit(entity);
    }
    
    @Override
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public void remove(Rating entity) {
        super.remove(entity);
    }

    @Override
    public Rating find(Object id) {
        return super.find(id);
    }

    @Override
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN})
    public List<Rating> findAll() {
        return super.findAll();
    }

    @Override
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN})
    public List<Rating> findRange(int[] range) {
        return super.findRange(range);
    }

    @Override
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN})
    public int count() {
        return super.count();
    }
    
}
